package ru.isys.generator;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerConfigurationException;

import ru.isys.activity.MainActivity;
import ru.isys.xml.XmlItem;
import ru.isys.xml.util.builder.ModelBuilder;
import ru.isys.xml.util.prepare.PrepareUtil;

public class ParserImpl implements Parser
{
	MainActivity listener;
   
   @Override
	public List <XmlItem> getAppData(String wsdlIn, String ns) throws TransformerConfigurationException {
		
       List <XmlItem> items=new ArrayList<XmlItem>();
        try {
            
            PrepareUtil prepare = new PrepareUtil(wsdlIn, ns,ModelBuilder.getInstance());
            String preparedModel= prepare.prepareModel();           
            items=ModelBuilder.getInstance().getXmlItems();
            listener.addMessage(preparedModel);
            
            
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(new JFrame(), "Ошибка открытия файла", "Ошибка",
			        JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		} catch (XMLStreamException e) {
			JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "Ошибка",
			        JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
        catch (ParserConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
		return items;
    }
   @Override
   public void setListener(MainActivity listener) {
    
    this.listener  = listener;
    
   }

}
